package com.project.service.imp;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.ClassBean;
import com.project.bean.RoomBean;
import com.project.bean.StudentBean;
import com.project.mapper.IClassMapper;
import com.project.mapper.IRoomMapper;
import com.project.mapper.IStudentMapper;
import com.project.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional
public class StudentServiceImp implements IStudentService{
    @Autowired
    private IStudentMapper studentMapper;
    @Autowired
    private IRoomMapper roomMapper;
    @Autowired
    private IClassMapper classMapper;

    @Override
    public IPage<StudentBean> cutFind(Integer pageNum, String stuName, String stuClassName, String stuRoom) {
        return studentMapper.cutFind(new Page(pageNum,2),stuName,stuClassName,stuRoom);
    }

    @Override
    public void add(StudentBean studentBean) {
        studentMapper.insert(studentBean);
    }

    @Override
    public void changeRoom(Integer stuID, Integer newRoomID) {
        StudentBean studentBean = studentMapper.selectById(stuID);
        studentBean.setStuRoomID(newRoomID);
        studentMapper.updateById(studentBean);

    }

    @Override
    public void delStudent(Integer stuID) {
        studentMapper.deleteById(stuID);
    }

    @Override
    public StudentBean findByID(Integer stuID) {
        StudentBean studentBean =  studentMapper.selectById(stuID);
        QueryWrapper qwClass = new QueryWrapper();
        qwClass.eq("pk_classid",studentBean.getStuClassID());
        ClassBean classBean = classMapper.selectOne(qwClass);
        studentBean.setStuClass(classBean);

        QueryWrapper qwRoom = new QueryWrapper();
        qwRoom.eq("pk_roomid",studentBean.getStuClassID());
        RoomBean roomBean = roomMapper.selectOne(qwRoom);
        studentBean.setStuRoom(roomBean);
        return studentBean;
    }
}
